#include "px_rendersystem.h"
#include "../utility/px_system.h"
struct PXEGL * create_PXEGL(struct PXVIEW * view)
{
	struct PXEGL * pEGL = (struct PXEGL *)malloc(sizeof(struct PXEGL));
	BOOL bSuccess = FALSE;
	do 
	{
		EGLDisplay eglDisplay;
		EGLint nMajor, nMinor;
		const EGLint aConfigAttribs[] =
		{
			EGL_LEVEL,				0,
			EGL_SURFACE_TYPE,		EGL_WINDOW_BIT,
			EGL_RENDERABLE_TYPE,	EGL_OPENGL_ES2_BIT,
			EGL_NATIVE_RENDERABLE,	EGL_FALSE,
			EGL_DEPTH_SIZE,			16,
			EGL_NONE,
		};
		EGLint iConfigs;
		EGLConfig eglConfig;
		EGLContext eglContext;
		EGLSurface eglSurface;
		PX_BREAK_IF(! pEGL);
		pEGL->m_eglNativeWindow = view->hwind;
		pEGL->m_eglNativeDisplay = GetDC(pEGL->m_eglNativeWindow);	
		PX_BREAK_IF(EGL_NO_DISPLAY == (eglDisplay = eglGetDisplay(pEGL->m_eglNativeDisplay)));	
		PX_BREAK_IF(EGL_FALSE == eglInitialize(eglDisplay, &nMajor, &nMinor) || 1 != nMajor);	
		PX_BREAK_IF(EGL_FALSE == eglChooseConfig(eglDisplay, aConfigAttribs, &eglConfig, 1, &iConfigs) 
			|| (iConfigs != 1));
		eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL);
		PX_BREAK_IF(EGL_NO_CONTEXT == eglContext);
		eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, pEGL->m_eglNativeWindow, NULL);
		PX_BREAK_IF(EGL_NO_SURFACE == eglSurface);

		PX_BREAK_IF(EGL_FALSE == eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext));

		pEGL->m_eglDisplay = eglDisplay;
		pEGL->m_eglConfig  = eglConfig;
		pEGL->m_eglContext = eglContext;
		pEGL->m_eglSurface = eglSurface;
		bSuccess = TRUE;
	} while (0);

	if (! bSuccess)
	{
		PX_SAFE_FREE(pEGL);  
	}

	return pEGL;
}

static void destroyEGLContext(EGLDisplay* display, EGLContext* context, EGLSurface* surface) {
    if (*display != EGL_NO_DISPLAY) {
        eglMakeCurrent(*display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

        if (*context != EGL_NO_CONTEXT) {
            eglDestroyContext(*display, *context);
        }

        if (*surface != EGL_NO_SURFACE) {
            eglDestroySurface(*display, *surface);
        }

        eglTerminate(*display);
    }

    *display = EGL_NO_DISPLAY;
    *context = EGL_NO_CONTEXT;
    *surface = EGL_NO_SURFACE;
}

void destroy_PXEGL(struct PXEGL *pxEGL)
{
	destroyEGLContext(&pxEGL->m_eglDisplay,&pxEGL->m_eglContext,&pxEGL->m_eglSurface); 
}

void swap_buffers(struct PXEGL * pxEGL)
{
	PX_RETURN_NULL(pxEGL);
	if (EGL_NO_DISPLAY != pxEGL->m_eglDisplay)
	{
		eglSwapBuffers(pxEGL->m_eglDisplay, pxEGL->m_eglSurface);
	}
}

PXBOOL init_opengl()
{
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glClearDepthf(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);


	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, SCREEN_WIDTH/SCREEN_HEIGHT, 0.5f, 1500.0f);

	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();
	gluLookAt( SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 500.0f,
		SCREEN_WIDTH/2,SCREEN_HEIGHT/2, 0,
		0.0f, 1.0f, 0.0f);	

	return PXTURE;	
}
